import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLock1 {
    public static void main(String[] args) {
        int executeCount = 2;
        long startTime = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(executeCount);

        ReentrantLock lock = new ReentrantLock();
        for(int index = 0;index < executeCount;index++) {
            System.out.println(index);
            new Thread(() -> {
                System.out.println("current thread name " + Thread.currentThread().getName() + " start");
                lock.lock();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
                System.out.println("current thread name " + Thread.currentThread().getName() + " end");
                countDownLatch.countDown();
            }).start();
        }
            try {
                countDownLatch.await();
            } catch(InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("cost time " + (System.currentTimeMillis()-startTime));

            ReentrantLock fareLock = new ReentrantLock(true);
            for(int index = 0;index < 5; index++) {
                new Thread(() -> {
                    for(int num = 1; num <= 2;num++) {
                        fareLock.lock();
                    try {
                        System.out.println("current thread name " + Thread.currentThread().getName() + " num=" + num);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        fareLock.unlock();
                    }
                }
                }).start();
            }
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                System.out.println("---------------------------------");
            }
          
            ReentrantLock unFareLock = new ReentrantLock(false);
            for(int index = 0;index < 5; index++) {
                new Thread(() -> {
                    for(int num = 1; num <= 2;num++) {
                        unFareLock.lock();
                    try {
                        System.out.println("current thread name " + Thread.currentThread().getName() + " num=" + num);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        unFareLock.unlock();
                    }
                }
                }).start();
            }
        }
}
